package com.woniu.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.model.dto.YClassTypeDto;
import com.woniu.model.form.ClassTypeAddForm;
import com.woniu.model.form.PageClassTypeForm;
import com.woniu.model.form.PageForm;
import com.woniu.model.model.YClassType;
import com.woniu.model.param.ClassTypeAddParam;
import com.woniu.model.param.PageClassTypeParam;
import com.woniu.model.param.PageParam;
import com.woniu.service.YClassTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yClassType")
@Api(tags = "课程类别接口")
public class YClassTypeController {

    @Resource
    private YClassTypeService classTypeService;


    /**
     * 新增课程类别
     *
     * @param form: typeName：类别名
     * @return 一个YClassType对象
     * @throws YogaException
     */
    @PostMapping("/addType")
    @ApiOperation(value = "新增课程类别的接口")
    public Result add(@RequestBody ClassTypeAddForm form) throws YogaException {
        YClassType type = classTypeService.addBookType(BeanUtil.toBean(form, ClassTypeAddParam.class));
        return Result.newInstance(null).setCode(200).setMsg("新增成功").setData(type);
    }


    /**
     * 禁用课程类别
     *
     * @param classTypeId：类别id
     * @return
     * @throws YogaException
     */
    @GetMapping("/disable")
    @ApiOperation(value = "禁用课程类别")
    public Result disable(Integer classTypeId) throws YogaException {
        classTypeService.disable(classTypeId);
        return Result.newInstance(null).setCode(200).setMsg("禁用成功");
    }


    /**
     * 删除课程类别
     *
     * @param classTypeId：类别id
     * @return
     * @throws YogaException
     */
    @GetMapping("/deleteType")
    @ApiOperation(value = "删除课程类别")
    public Result deleteType(Integer classTypeId) throws YogaException {
        classTypeService.deleteType(classTypeId);
        return Result.newInstance(null).setCode(200).setMsg("删除成功");
    }


    /**
     * 启用课程类别
     *
     * @param classTypeId:类别id
     * @return
     * @throws Exception
     */
    @GetMapping("/enable")
    @ApiOperation(value = "启用课程类别")
    public Result enable(Integer classTypeId) throws Exception {
        classTypeService.enable(classTypeId);
        return Result.newInstance(null).setCode(200).setMsg("启用成功");
    }


    /**
     * 通过多个类型ID查询类型信息
     *
     * @param id
     * @return
     * @throws YogaException
     */
    @PostMapping("/listByClassType")
    @ApiOperation(value = "通过多个类型ID查询类型信息")
    public Result<List<YClassTypeDto>> listByClassType(@RequestBody Set<Integer> id) throws YogaException {
        List<YClassType> classTypes = classTypeService.listByIds(id);
        return Result.newListInstance(YClassTypeDto.class).setData(BeanUtil.copyToList(classTypes, YClassTypeDto.class));
    }

    /**
     * 通过ID查询一个类型回去
     *
     * @param id:一个类型id
     * @return
     * @throws YogaException
     */
    @GetMapping("/byId/{id}")
    @ApiOperation(value = "通过ID查询一个类型回去")
    public Result<YClassTypeDto> byId(@PathVariable("id") Integer id) throws YogaException {
        YClassType type = classTypeService.getById(id);
        return Result.newInstance(YClassTypeDto.class).setData(BeanUtil.toBean(type, YClassTypeDto.class));
    }


    /**
     * 分页查询
     *
     * @param form pageNum; pageSize;
     * @return
     * @throws YogaException
     */
    @GetMapping("/loadPageClassType")
    @ApiOperation(value = "分页查询接口")
    public Result loadPageClassType( PageClassTypeForm form) throws YogaException {
        Page<YClassType> page = classTypeService.loadPageClassType(BeanUtil.toBean(form, PageClassTypeParam.class));
        return Result.newPageInstance(YClassType.class).setMsg("查询成功").setCode(200).setData(page);
    }

    /**
     * 查询所有课程类型
     *
     * @return
     * @throws YogaException
     */
    @GetMapping("/loadClassTypeList")
    @ApiOperation(value = "查询所有课程类型接口")
    public Result<List<YClassType>> loadClassTypeList() throws YogaException {
        List<YClassType> list = classTypeService.loadClassTypeList();
        return Result.newListInstance(YClassType.class).setMsg("查询成功").setCode(200).setData(list);
    }



}

